const app = getApp()
Page({
  data: {
    listKt: [],
    listXtid: [],
    ffrom: '',
    teaId: ''
  },
  onLoad: function(options) {
    const that = this
    if (options.from == 'stuDs') {
      that.setData({
        ffrom: 'stuDs',
        teaId: options._id
      })
    }
    if (options.from == 'stuWd') {
      that.setData({
        ffrom: 'stuWd'
      })
    }
    if (options.from == 'teaSy') {
      that.setData({
        ffrom: 'teaSy'
      })
    }
  },
  onShow: function() {
    const that = this
    that.setData({
      listKt: []
    })
    if (that.data.ffrom == 'stuDs') {
      wx.cloud.database({
        env: 'gtss-2b1812'
      }).collection('Topic').field({
        _id: true,
        ktmc: true,
        teaName: true
      }).limit(20).where({
        teaId: that.data.teaId,
        ktzt: 0
      }).get().then(res => {
        that.setData({
          listKt: res.data
        })
      })
    }
    if (that.data.ffrom == 'stuWd') {
      wx.cloud.database({
        env: 'gtss-2b1812'
      }).collection('Option').where({
        userId: app.globalData.userId
      }).get().then(res => {
        that.setData({
          listXtid: res.data
        })
        for (var i = 0; i < that.data.listXtid.length; i++) {
          wx.cloud.database({
            env: 'gtss-2b1812'
          }).collection('Topic').doc(that.data.listXtid[i].topicId).get().then(res => {
            that.setData({
              listKt: that.data.listKt.concat(res.data)
            })
          })
        }
      })
    }
    if (that.data.ffrom == 'teaSy') {
      wx.cloud.database({
        env: 'gtss-2b1812'
      }).collection('Topic').field({
        _id: true,
        ktmc: true,
        ktzt: true
      }).limit(20).where({
        teaId: app.globalData.userId
      }).get().then(res => {
        that.setData({
          listKt: res.data
        })
      })
    }
  },
  funKtxq: function(e) {
    const that = this
    if (that.data.ffrom == 'stuDs' || that.data.ffrom == 'stuWd') {
      wx.navigateTo({
        url: "../stuKtxq/stuKtxq?_id=" + e.currentTarget.dataset.id + '&ffrom=' + this.data.ffrom
      })
    }
    if (that.data.ffrom == 'teaSy') {
      wx.navigateTo({
        url: "../teaKtgl/teaKtgl?_id=" + e.currentTarget.dataset.id
      })
    }
  }
})